package com.hmdp.service.impl;

import cn.hutool.json.JSONUtil;
import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jodd.util.StringUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

import static com.hmdp.utils.RedisConstants.Shop_Type_List_Key;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<ShopType> queryList() {
        // 查询redis缓存
        String shopTypeListStr = stringRedisTemplate.opsForValue().get(Shop_Type_List_Key);
        if (StringUtil.isNotBlank(shopTypeListStr)) {
            // 存在 转换为集合 返回
            return  JSONUtil.toList(shopTypeListStr,ShopType.class);
        }
        // 不存在 去数据库查询返回
        List<ShopType> list = lambdaQuery().orderByAsc(ShopType::getCreateTime).list();
        // 保存数据到redis中
        stringRedisTemplate.opsForValue().set(Shop_Type_List_Key,JSONUtil.toJsonStr(list));
        return list;
    }
}
